package com.zzx.test.question;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class LeetCode3 {

    public static void main(String[] args) {
//        System.out.println("一年中的第几天:" + dayOfYear("2020-03-01"));
//        System.out.println("分割平衡字符串:" + balancedStringSplit("RLRRLLRLRL"));
//        System.out.println("整数的各位积和之差:" + subtractProductAndSum(10));
//        System.out.println("用户分组:" + groupThePeople(new int[]{3, 3, 3, 3, 3, 1, 3}));
//        System.out.println("解压缩编码列表:" + Arrays.toString(decompressRLElist(new int[]{1, 1, 2, 3})));
//        System.out.println("拥有最多糖果的孩子:" + kidsWithCandies(new int[]{2, 3, 5, 1, 3}, 3));
//        System.out.println("在既定时间做作业的学生人数:" + busyStudent(new int[]{1, 2, 3}, new int[]{3, 2, 7}, 4));
//        System.out.println("所有奇数长度子数组的和:" + sumOddLengthSubarrays(new int[]{1, 4, 2, 5, 3}));
        System.out.println(maxDepth("(1)+((2))+(((3)))"));
    }

    static int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static int dayOfYear(String date) {
        int res = 0;

        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = Integer.parseInt(date.substring(8));

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) days[1] = 29;

        for (int i = 0; i < month - 1; i++) {
            res += days[i];
        }

        return res + day;
    }

    public static int balancedStringSplit(String s) {
        int res = 0, n = 0;
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (c == 'L') {
                n++;
            } else if (c == 'R') {
                n--;
            }
            if (n == 0) res++;
        }
        return res;
    }

    public static int subtractProductAndSum(int n) {
        int j = 1, h = 0;
        while (n != 0) {
            j = j * (n % 10);
            h = h + (n % 10);
            n /= 10;
        }
        return j - h;
    }

    public static List<List<Integer>> groupThePeople(int[] groupSizes) {
        List<List<Integer>> listList = new ArrayList<>();
        HashMap<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < groupSizes.length; i++) {
            List<Integer> list = map.getOrDefault(groupSizes[i], new ArrayList<>());
            list.add(i);
            map.put(groupSizes[i], list);
        }
        for (Integer key : map.keySet()) {
            List<Integer> list = map.get(key), cur = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                cur.add(list.get(i));
                if ((i + 1) % key == 0) {
                    listList.add(cur);
                    cur = new ArrayList<>();
                }
            }
        }
        return listList;
    }

    public static int[] decompressRLElist(int[] nums) {
        int len = 0, index = 0;
        for (int i = 0; i < nums.length; i += 2) {
            len += nums[i];
        }
        int[] res = new int[len];
        for (int i = 0; i < nums.length; i += 2) {
            for (int j = 0; j < nums[i]; j++) {
                res[index++] = nums[i + 1];
            }
        }
        return res;
    }

    public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
        List<Boolean> res = new ArrayList<>(candies.length);
        int max = 0;
        for (int candy : candies) {
            if (candy > max) {
                max = candy;
            }
        }
        for (int candy : candies) {
            if (candy + extraCandies >= max) {
                res.add(true);
            } else {
                res.add(false);
            }
        }
        return res;
    }

    public static int busyStudent(int[] startTime, int[] endTime, int queryTime) {
        int res = 0;
        for (int i = 0; i < startTime.length; i++) {
            if (startTime[i] <= queryTime && endTime[i] >= queryTime) {
                res += 1;
            }
        }
        return res;
    }

    public static int sumOddLengthSubarrays(int[] arr) {
        if (arr.length == 1) {
            return arr[0];
        }
        int res = 0, len = 1;
        int arrLen = arr.length;
        while (len <= arrLen) {
            int count = arrLen - len + 1;
            for (int i = 0; i < count; i++) {
                for (int j = i; j < len + i; j++) {
                    res += arr[j];
                }
            }
            len += 2;
        }
        return res;
    }

    public static int maxDepth(String s) {
        int left = 0, res = 0;
        char[] charArray = s.toCharArray();
        for (char c : charArray) {
            if (c == '(') {
                left++;
            } else if (c == ')') {
                left--;
            }
            res = Math.max(left, res);
        }
        return res;
    }

}
